#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/21 10:03
# @Author : 闫峰
# @File    : ceshi1.py
import json
import threading
import time
from datetime import datetime

import pymongo

from utils.mysqlUtils.mongodb_utils import MongoDBLink
import os

from common.setting import ensure_path_sep
from utils.logging_tool.log_control import LogHandler
from utils.mysqlUtils.db_pool import Database
from utils.mysqlUtils.sql_server_utils import MSSQL
from concurrent.futures import ThreadPoolExecutor, wait, as_completed
from functools import partial

current_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_path)
path_list = current_path.split("\\")
dis_path = "\\".join(path_list[-2:-1])


def remove_file(info_path):
    if os.path.exists(info_path):
        os.remove(info_path)
    else:
        print(f"{info_path} 不存在")


info_path = ensure_path_sep(f"\\logs\\info-{dis_path}{time.time()}")
error_path = ensure_path_sep(f"\\logs\\\error-{dis_path}{time.time()}.log")
warning_path = ensure_path_sep(f"\\logs\\warning-{dis_path}{time.time()}.log")
remove_file(info_path)
remove_file(error_path)
remove_file(warning_path)
INFO = LogHandler(info_path, level='info')
ERROR = LogHandler(error_path, level='error')
WARNING = LogHandler(warning_path, level='warning')


class TestChange:
    mapping = {
        "packageLength": "Length",
        "packageWidth": "Width",
        "packageHeight": "Height"
    }

    def test_allocation_bill(self):
        # erp_db = MSSQL(ensure_path_sep("\\config\\config.yaml"), "sqlserver_db_erp_base")
        mysql_db = Database(ensure_path_sep("\\testcases\\erp\\responsible_inventory\\config.yaml"),
                            "irobotbox_datacenter")
        insert_db = Database(ensure_path_sep("\\config\\ceshi_mysql_config.yaml"), "log_db")
        # 遍历RB_Inventory_DetailLog库，验证allocation_bill
        get_BillNumber = f"""
        SELECT BillNumber,AllocationOrderNo,SKU  FROM znkdceshilog.`allocation_bill`
        """
        actual_sql = """
        SELECT BillNumber,AllocationOrderNo,SKU FROM zhcxkj_datacenter.allocation_bill  
        WHERE AddTime >='2023-08-01 00:00:00' AND AddTime<= '2023-08-31 23:59:59'
        """
        expect_value = insert_db.fetch_all_tuple(get_BillNumber, ())
        actual_value = mysql_db.fetch_all_tuple(actual_sql,())
        expect_value_set = set(expect_value)
        actual_vakue_set = set(actual_value)
        print()
        if expect_value_set == actual_vakue_set:
            pass
        else:
            ERROR.logger.error(f"缺失{len(expect_value_set-actual_vakue_set)}")
            print(expect_value_set-actual_vakue_set)
            print(actual_vakue_set - expect_value_set)
            print()






if __name__ == '__main__':
    # TestChange().test_allocation_bill()
    t = TestChange()
    t.test_allocation_bill()

